package cn.edu.neosoft.sunbo509.finefood.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.PurchaseBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PurchaseActivity extends AppCompatActivity {
    private TextView tv_shopname,tv_foodname,tv_price,tv_timeorder,tv_sum;
    private EditText et_username,et_address,et_number;
    private Spinner sp_time;
    private Retrofit retrofit;
    private int food_id,shop_id,num;
    private String user_id;
    private String foodname,timesent,shopname;
    private double foodprice,sum;
    private Service service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase);
        getDate();
        InitView();
        InitSpinner();
        show();
    }
//接收从DishesdetailActivity传递来的数据
    private void getDate() {
        Intent intent=getIntent();
        user_id=intent.getStringExtra("userid");
        food_id=intent.getIntExtra("foodid",1);
        shop_id=intent.getIntExtra("shopid",1);
        foodname=intent.getStringExtra("foodname");
        foodprice=intent.getDoubleExtra("foodprice",1.0);
        shopname=intent.getStringExtra("shopname");
    }

    private void InitView() {
        tv_shopname=(TextView)findViewById(R.id.tv_shopname);
        tv_foodname=(TextView)findViewById(R.id.tv_foodname);
        tv_price=(TextView)findViewById(R.id.tv_price);
        tv_timeorder=(TextView)findViewById(R.id.tv_timeorder);
        tv_sum=(TextView) findViewById(R.id.tv_sum);
        et_address=(EditText)findViewById(R.id.et_address);
        et_username=(EditText)findViewById(R.id.et_username);
        sp_time=(Spinner)findViewById(R.id.sp_time);
        et_number=(EditText)findViewById(R.id.et_number);
        et_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()>0){
                    num=Integer.valueOf(et_number.getText().toString());
                    sum=num*foodprice;
                    tv_sum.setText(sum+"");
                }
            }
        });
    }

    public void InitSpinner(){
        String[] times = {"8:00-9:00","11:00-12:00" ,"17:00-18:00" ,"20:00-21:00" };//定义数据源         方法一
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, times);//实例化ArrayAdapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 设置下拉按钮样式
        sp_time.setAdapter(adapter);  // 将spinner与ArrayAdapter绑定
        sp_time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l){
                timesent=adapterView.getItemAtPosition(i).toString();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    public void show(){
        SimpleDateFormat formatter =  new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss");
        Date curDate =new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        tv_shopname.setText(shopname);
        tv_foodname.setText(foodname);
        tv_price.setText("￥"+foodprice+"元");
        tv_timeorder.setText(str);
    }

    public void click(View view) {
        String username = et_username.getText().toString().trim();
        String address=et_address.getText().toString().trim();
        String number=et_number.getText().toString().trim();
        if(TextUtils.isEmpty(username)||TextUtils.isEmpty(address)||TextUtils.isEmpty(number)
        ){
            Toast.makeText(getApplicationContext(),"您有未完成的信息",Toast.LENGTH_SHORT).show();
            return;
        }
        else{
            Purchase();
        }
    }

    public void Purchase(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
         service=retrofit.create(Service.class);
        Call<PurchaseBean>call=service.getpurchaseValue(Integer.valueOf(user_id),food_id,num,sum,timesent);
        call.enqueue(new Callback<PurchaseBean>() {
            @Override
            public void onResponse(Call<PurchaseBean> call, Response<PurchaseBean> response) {
                if(response.body().getSuccess().equals("1"))
                    Toast.makeText(getApplicationContext(), "购买成功", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(getApplicationContext(),"购买失败",Toast.LENGTH_SHORT).show();

        }
            @Override
            public void onFailure(Call<PurchaseBean> call, Throwable t) {
                Log.e("error",t.getMessage());
            }
        });
    }

}



